﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Page_WebNavModify : BasePage
{
    t_webnav model = new t_webnav();
    t_webnavBLL bll = new t_webnavBLL();

    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return Request.QueryString["Id"] == null ? 0 : Convert.ToInt32(Request.QueryString["Id"]);
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return Request.QueryString["Action"] == null ? "" : Request.QueryString["Action"];
        }
    }

    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        base.CheckUserPermission("003010");
        if (!IsPostBack)
        {
            BindDropDownList();
            getDate();
        }
    }
    #endregion

    #region 加载数据
    private void getDate()
    {
        if (Action == "Update" || Action == "Detail")
        {
            model = bll.GetModel(Id);
            this.ddl_Parent.SelectedValue = model.ParentId.ToString();
            this.txt_Title.Text = model.Title;
            this.txt_Code.Text = model.Code;
            this.txt_OrderNo.Text = model.OrderNo;

            if (model.Code.ToLower() == "index" || model.Code.ToLower() == "news")
            {
                this.ddl_Parent.Enabled = false;
                this.txt_Code.Enabled = false;
            }
        }
    }

    private void BindDropDownList()
    {
        List<t_webnav> list = bll.GetModelList(" ParentId=0 and Code<>'Index' ");
        this.ddl_Parent.DataTextField = "Title";
        this.ddl_Parent.DataValueField = "WebNavId";
        this.ddl_Parent.DataSource = list;
        this.ddl_Parent.DataBind();

        this.ddl_Parent.Items.Insert(0, new ListItem("<无>", "0"));
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        if (Action == "Update")
        {
            model = bll.GetModel(Id);
        }
        else if (Action == "Add")
        {
            model = new t_webnav();
        }
        else
        {
            base.Alert("操作出现异常！");
            return;
        }

        model.ParentId = Convert.ToInt32(this.ddl_Parent.SelectedValue);
        model.Title = this.txt_Title.Text.Trim();
        model.Code = this.txt_Code.Text.Trim();
        model.OrderNo = (this.txt_OrderNo.Text.Trim() == "" ? "0" : this.txt_OrderNo.Text.Trim());
        model.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.UpdateUserId = base.CurrentSession.UserID;
        model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);

        if (Action == "Update")
        {
            bll.Update(model);
        }
        else if (Action == "Add")
        {
            model.CreateTime = model.UpdateTime;
            model.CreateUserId = model.UpdateUserId;
            bll.Add(model);
        }
        base.Redirect("操作成功！", "WebNavList.aspx");
    }
    #endregion

}
